package com.ai.ubssapp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class HttpRequestParser
{
  public static Request parse(String urlString)
  {
    return parse(urlString, "utf-8");
  }

  public static Request parse(String urlString, String enc)
  {
    if ((urlString == null) || (urlString.length() == 0)) {
      return new Request();
    }
    int questIndex = urlString.indexOf(63);
    if (questIndex == -1) {
      return new Request(urlString);
    }
    String url = urlString.substring(0, questIndex);
    String queryString = urlString.substring(questIndex + 1, urlString.length());
    return new Request(url, getParamsMap(queryString, enc));
  }

  private static Map<String, String[]> getParamsMap(String queryString, String enc) {
    Map paramsMap = new HashMap();
    if ((queryString != null) && (queryString.length() > 0)) { int lastAmpersandIndex = 0;
      int ampersandIndex;
      do {
        ampersandIndex = queryString.indexOf(38, lastAmpersandIndex) + 1;
        String subStr;
        if (ampersandIndex > 0) {
          subStr = queryString.substring(lastAmpersandIndex, ampersandIndex - 1);
          lastAmpersandIndex = ampersandIndex;
        } else {
          subStr = queryString.substring(lastAmpersandIndex);
        }
        String[] paramPair = subStr.split("=");
        String param = paramPair[0];
        String value = paramPair.length == 1 ? "" : paramPair[1];
        try {
          value = URLDecoder.decode(value, enc);
        }
        catch (UnsupportedEncodingException ignored)
        {
        }
        String[] newValues;
        if (paramsMap.containsKey(param)) {
          String[] values = (String[])paramsMap.get(param);
          int len = values.length;
          newValues = new String[len + 1];
          System.arraycopy(values, 0, newValues, 0, len);
          newValues[len] = value;
        } else {
          newValues = new String[] { value };
        }
        paramsMap.put(param, newValues);
      }while (ampersandIndex > 0);
    }
    return paramsMap;
  }

  public static class Request implements HttpServletRequest
  {
    private String requestURI;
    private Map<String, String[]> parameterMap;

    Request() {
      this("");
    }

    Request(String requestURI) {
      this.requestURI = requestURI;
      this.parameterMap = new HashMap();
    }

    Request(String requestURI, Map<String, String[]> parameterMap) {
      this.requestURI = requestURI;
      this.parameterMap = parameterMap;
    }

    public String getParameter(String name)
    {
      String[] values = (String[])this.parameterMap.get(name);
      if ((values != null) && (values.length > 0)) {
        return values[0];
      }
      return null;
    }

    public Enumeration<String> getParameterNames()
    {
      return Collections.enumeration(this.parameterMap.keySet());
    }

    public String[] getParameterValues(String name)
    {
      return (String[])this.parameterMap.get(name);
    }

    public String getRequestURI()
    {
      return this.requestURI;
    }

    public Map<String, String[]> getParameterMap()
    {
      return this.parameterMap;
    }

    public String toString()
    {
      StringBuilder buf = new StringBuilder();
      buf.append("{");
      buf.append("\n  url = ").append(this.requestURI);
      buf.append("\n  paramsMap = {");
      if (this.parameterMap.size() > 0) {
        for (Map.Entry e : this.parameterMap.entrySet()) {
          buf.append((String)e.getKey()).append("=").append(Arrays.toString((Object[])e.getValue())).append(",");
        }
        buf.deleteCharAt(buf.length() - 1);
      }
      buf.append("}\n}");
      return buf.toString();
    }

    public String getAuthType()
    {
      return null;
    }

    public String getContextPath()
    {
      return null;
    }

    public Cookie[] getCookies()
    {
      return null;
    }

    public long getDateHeader(String arg0)
    {
      return 0L;
    }

    public String getHeader(String arg0)
    {
      return null;
    }

    public Enumeration getHeaderNames()
    {
      return null;
    }

    public Enumeration getHeaders(String arg0)
    {
      return null;
    }

    public int getIntHeader(String arg0)
    {
      return 0;
    }

    public String getMethod()
    {
      return null;
    }

    public String getPathInfo()
    {
      return null;
    }

    public String getPathTranslated()
    {
      return null;
    }

    public String getQueryString()
    {
      return null;
    }

    public String getRemoteUser()
    {
      return null;
    }

    public StringBuffer getRequestURL()
    {
      return null;
    }

    public String getRequestedSessionId()
    {
      return null;
    }

    public String getServletPath()
    {
      return null;
    }

    public HttpSession getSession()
    {
      return null;
    }

    public HttpSession getSession(boolean arg0)
    {
      return null;
    }

    public Principal getUserPrincipal()
    {
      return null;
    }

    public boolean isRequestedSessionIdFromCookie()
    {
      return false;
    }

    public boolean isRequestedSessionIdFromURL()
    {
      return false;
    }

    public boolean isRequestedSessionIdFromUrl()
    {
      return false;
    }

    public boolean isRequestedSessionIdValid()
    {
      return false;
    }

    public boolean isUserInRole(String arg0)
    {
      return false;
    }

    public Object getAttribute(String arg0)
    {
      return null;
    }

    public Enumeration getAttributeNames()
    {
      return null;
    }

    public String getCharacterEncoding()
    {
      return null;
    }

    public int getContentLength()
    {
      return 0;
    }

    public String getContentType()
    {
      return null;
    }

    public ServletInputStream getInputStream()
      throws IOException
    {
      return null;
    }

    public String getLocalAddr()
    {
      return null;
    }

    public String getLocalName()
    {
      return null;
    }

    public int getLocalPort()
    {
      return 0;
    }

    public Locale getLocale()
    {
      return null;
    }

    public Enumeration getLocales()
    {
      return null;
    }

    public String getProtocol()
    {
      return null;
    }

    public BufferedReader getReader()
      throws IOException
    {
      return null;
    }

    public String getRealPath(String arg0)
    {
      return null;
    }

    public String getRemoteAddr()
    {
      return null;
    }

    public String getRemoteHost()
    {
      return null;
    }

    public int getRemotePort()
    {
      return 0;
    }

    public RequestDispatcher getRequestDispatcher(String arg0)
    {
      return null;
    }

    public String getScheme()
    {
      return null;
    }

    public String getServerName()
    {
      return null;
    }

    public int getServerPort()
    {
      return 0;
    }

    public boolean isSecure()
    {
      return false;
    }

    public void removeAttribute(String arg0)
    {
    }

    public void setAttribute(String arg0, Object arg1)
    {
    }

    public void setCharacterEncoding(String arg0)
      throws UnsupportedEncodingException
    {
    }
  }
}
